package cn.itcast.dewu.config;

import cn.itcast.dewu.interceptor.LoginInterceptor;
import cn.itcast.dewu.interceptor.TokenRefushInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    private final StringRedisTemplate redisTemplate;

    public InterceptorConfig(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns(
                        "/user/code",
                        "/user/login",
                        "/goods/**",
                        "/goods-type/**",
                        "/voucher/**"
                ).order(1);
        //对所有的路径都拦截,主要刷新token时间
        registry.addInterceptor(new TokenRefushInterceptor(redisTemplate)).order(0);
    }
}
